package com.one.consumer.reserve.ui.activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.google.gson.Gson;
import com.one.common.config.CMemoryData;
import com.one.common.config.RouterPath;
import com.one.common.model.bean.CityBean;
import com.one.common.model.bean.UserInfoBean;
import com.one.common.view.base.BaseActivity;
import com.one.common.view.widget.MyTitleBar;
import com.one.consumer.BaseActivityC;
import com.one.consumer.R;
import com.one.consumer.reserve.model.bean.YouhuijuanResaultBean;
import com.one.consumer.reserve.model.bean.YouhuiquanBean;
import com.one.consumer.reserve.presenter.YouhuijuanPresenter;
import com.one.consumer.reserve.ui.adapter.BaseRecyclerAdapter;
import com.one.consumer.reserve.ui.adapter.SmartViewHolder;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.common.util.LogUtil;
import org.xutils.http.RequestParams;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

@Route(path = RouterPath.youhuijuan_list)
public class YouhuijuanListActivity extends BaseActivityC<YouhuijuanPresenter> implements AdapterView.OnItemClickListener {

    private List<YouhuiquanBean> datas = new ArrayList<>();
    BaseRecyclerAdapter<YouhuiquanBean> mAdapter;

    @BindView(R.id.list)
    RecyclerView mRecyclerView;

    @Override
    protected void initPresenter() {
        mPresenter = new YouhuijuanPresenter(this,this);
    }

    @Override
    public int getLayoutResId() {
        return R.layout.activity_youhuijuan_list;
    }

    @Override
    protected void initTitle() {
        super.initTitle();
        getMyTitleBar().setMode(MyTitleBar.Mode.BACK_TITLE)
                .setTitleText("优惠券");
    }

    @Override
    public void initView() {
        super.initView();
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setAdapter(mAdapter = new BaseRecyclerAdapter<YouhuiquanBean>(new ArrayList<YouhuiquanBean>(), R.layout.listitem_showpoint_sample, this) {
            @Override
            protected void onBindViewHolder(SmartViewHolder holder, YouhuiquanBean model, int position) {
//                    holder.text(R.id.id, model.getOrderNum());
//                    holder.text(R.id.content, model.getPruductName());
                holder.text(R.id.user_name, model.getTitle());
                if(model.getRule()==1)
                    holder.text(R.id.price, "¥"+Math.round(model.getMoney()));
                else
                    holder.text(R.id.price, model.getMoney()+"折");
                holder.text(R.id.user_phone, model.getRule_remark());
                holder.text(R.id.user_date, "有效截至:"+model.getSx_at());
                holder.text(R.id.user_scene,"使用范围:"+model.getUse_scene());
//                holder.setVisibaled(R.id.user_phone, View.GONE);
//        holder.text(R.id.user_phone, model.getStartTime());
//                    holder.setBackGround(R.id.message_title_bar, model.isRead()==true?R.drawable.message_item_head:R.drawable.message_item_head_disable);
            }
        });
    }

    @Override
    public void initData() {
        super.initData();
        loadDatas();
    }

    private void loadDatas(){
        RequestParams params = new RequestParams("https://yinlong.yinweish.com/kmCouponController/v1.0/findMyCoupons");
        UserInfoBean bean = CMemoryData.getUserInfo();
        params.addBodyParameter("member_account",bean.getMember_account());
        params.addBodyParameter("member_id", bean.getMember_id()+"");
        params.addBodyParameter("member_token", bean.getMember_token());
        params.setCacheMaxAge(0);
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
//                LogUtil.e("onSuccess1 "+result+"----"+params.toString());
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String data = jsonObject.optString("data");
                    LogUtil.e(" onSuccess2 "+data);
                    YouhuijuanResaultBean datas = new Gson().fromJson(data, YouhuijuanResaultBean.class);
                    YouhuijuanListActivity.this.datas = datas.getData();
                    updateUI();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
            }
            @Override
            public void onCancelled(Callback.CancelledException cex) {}
            @Override
            public void onFinished() {}
        });
    }

    private void updateUI(){
        mAdapter.refresh(datas);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    }
}
